﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BAASearchLib;
using System.Threading;
using System.Diagnostics;

namespace GUI
{
    public partial class Form1 : Form
    {
        private IList<ResultItem> thelist = new BindingList<ResultItem>();
        private FolderList folderList;
        private DateType dateType = DateType.None;
        private DatePeriod datePeriod = DatePeriod.Exact;

        public Form1()
        {
            InitializeComponent();

            bindingSource1.DataSource = thelist;
            dataGridView1.DataSource = bindingSource1;

            Control.CheckForIllegalCrossThreadCalls = false;

            cBoxDate2.SelectedIndex = 0;

            checkedComboBox1.Items.Add("doc");
            checkedComboBox1.Items.Add("docx");
            checkedComboBox1.Items.Add("ppt");
            checkedComboBox1.Items.Add("pptx");
            checkedComboBox1.Enabled = false;
        }

        private void dataRepeater1_CurrentItemIndexChanged(object sender, EventArgs e)
        {

        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            folderList = new FolderList(@"C:\", flowLayoutPanel1.Controls);
            flowLayoutPanel1.Controls.Add(folderList);
        }



        private void btnSearch_Click(object sender, EventArgs e)
        {
            string[] paths = folderList.SearchPath;

            // -------------------------------------
            // Check the Date also
            // -something is wrong with the doc and ppt content search
            // -------------------------------------

            thelist.Clear();
            foreach (string path in paths)
            {
                BAASearch search; search = new BAASearch(path, txtFileName.Text,
                    txtAuthor.Text, dateType, dateTimePicker1.Value, datePeriod, txtFileType.Text, txtContent.Text, thelist);
                search.searchingFolderEvent += new SearchingFolderEvent(search_searchingFolderEvent);

                Thread seachThread = new Thread(new ThreadStart(search.Search));
                seachThread.SetApartmentState(ApartmentState.STA);
                seachThread.Start();
            }
        }

        private void search_searchingFolderEvent(string path)
        {
            statusStripSearch.Text = path;
        }

        private void cBoxDate1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cBoxDate1.SelectedIndex)
            {
                case 0:
                    dateType = DateType.Created;
                    break;
                case 1:
                    dateType = DateType.Modified;
                    break;
                default:
                    dateType = DateType.None;
                    break;
            }
        }

        private void cBoxDate2_SelectedIndexChanged(object sender, EventArgs e)
        {
            dateTimePicker1.Enabled = (cBoxDate2.SelectedIndex != 0);
            switch (cBoxDate2.SelectedIndex)
            {
                case 1:
                    datePeriod = DatePeriod.Exact;
                    break;
                case 2:
                    datePeriod = DatePeriod.Before;
                    break;
                case 3:
                    datePeriod = DatePeriod.After;
                    break;
                default:
                    datePeriod = DatePeriod.Exact;
                    break;
            }
        }

        private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                // Get first selected item
                DataGridViewRow row = ((DataGridView)sender).SelectedRows[0];
                if (row != null)
                {
                    // Open selected file
                    ResultFile r = (ResultFile)row.DataBoundItem;
                    System.Diagnostics.ProcessStartInfo PSI = new System.Diagnostics.ProcessStartInfo();
                    PSI.FileName = r.Location + "\\" + r.FileName;
                    PSI.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
                    System.Diagnostics.Process.Start(PSI);
                }
            }
            catch (Exception)
            {
                // do nothing
            }

        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button.Equals(MouseButtons.Right))
            {
                // Unselect all selected items
                foreach (DataGridViewRow r in ((DataGridView)sender).SelectedRows)
                {
                    r.Selected = false;
                }
                // Select the row which is close to the mouse
                dataGridView1.Rows[e.RowIndex].Selected = true;
            }
        }

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button.Equals(MouseButtons.Right) && thelist.Count > 0)
            {
                // Show context menu
                contextMenuStrip1.Show(this.dataGridView1, e.Location);
                Debug.WriteLine("Item: " + contextMenuStrip1.Items[0].Text);
            }
        }

        private void openFileStripMenuItem_Click(object sender, EventArgs e)
        {
            // Get first selected item
            DataGridViewRow row = dataGridView1.SelectedRows[0];
            if (row != null)
            {
                // Open selected file
                ResultFile r = (ResultFile)row.DataBoundItem;
                System.Diagnostics.ProcessStartInfo PSI = new System.Diagnostics.ProcessStartInfo();
                PSI.FileName = r.Location + "\\" + r.FileName;
                PSI.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
                System.Diagnostics.Process.Start(PSI);
            }
        }

        private void openContainerFolderStripMenuItem_Click(object sender, EventArgs e)
        {
            // Get first selected item
            DataGridViewRow row = dataGridView1.SelectedRows[0];
            if (row != null)
            {
                // Open selected file's container folder
                ResultFile r = (ResultFile)row.DataBoundItem;
                System.Diagnostics.Process.Start(r.Location);
            }
        }
    }
}
